<template>
  <div class="ai-assistant-page">
    <div class="page-header">
      <h3>ERP智能助手</h3>
      <el-button type="primary" @click="showAssistantDialog = true">
        打开智能助手
      </el-button>
    </div>

    <div class="assistant-features">
      <el-row :gutter="20">
        <el-col :span="8" v-for="feature in features" :key="feature.id">
          <el-card class="feature-card" @click="handleFeature(feature)">
            <div class="feature-icon">
              <el-icon :size="48" :color="feature.color">
                <component :is="feature.icon" />
              </el-icon>
            </div>
            <div class="feature-title">{{ feature.title }}</div>
            <div class="feature-desc">{{ feature.description }}</div>
            <div class="feature-actions">
              <el-button type="primary" size="small">立即使用</el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 智能助手对话框 -->
    <el-dialog
      v-model="showAssistantDialog"
      title="ERP智能助手"
      width="80%"
      :close-on-click-modal="false"
      class="assistant-dialog"
    >
      <div class="assistant-content">
        <div class="assistant-panel">
          <div class="assistant-suggestions">
            <div class="suggestions-title">推荐操作</div>
            <div 
              v-for="suggestion in suggestions"
              :key="suggestion.id"
              class="suggestion-item"
              @click="executeSuggestion(suggestion)"
            >
              <el-icon class="suggestion-icon"><component :is="suggestion.icon" /></el-icon>
              <div class="suggestion-content">
                <div class="suggestion-title">{{ suggestion.title }}</div>
                <div class="suggestion-desc">{{ suggestion.description }}</div>
              </div>
            </div>
          </div>
        </div>
        
        <div class="assistant-chat">
          <div class="chat-messages">
            <div 
              v-for="(msg, index) in assistantMessages"
              :key="index"
              :class="['message', msg.role]"
            >
              <div class="message-avatar">
                <el-avatar v-if="msg.role === 'user'" :icon="User" />
                <el-avatar v-else :icon="Service" style="background: #409EFF;" />
              </div>
              <div class="message-content">
                <div class="message-text">{{ msg.content }}</div>
                <div class="message-time">{{ formatTime(msg.timestamp) }}</div>
              </div>
            </div>
          </div>
          
          <div class="chat-input">
            <el-input
              v-model="assistantInput"
              placeholder="输入您的问题..."
              @keydown.enter="sendAssistantMessage"
            >
              <template #append>
                <el-button @click="sendAssistantMessage" :icon="Promotion">发送</el-button>
              </template>
            </el-input>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { 
  Document, Money, ShoppingCart, User, 
  Setting, Service, Promotion, TrendCharts,
  Box
} from '@element-plus/icons-vue'

const showAssistantDialog = ref(false)
const assistantInput = ref('')
const assistantMessages = ref([
  {
    role: 'assistant',
    content: '您好！我是ERP智能助手，可以帮您处理各种业务操作，有什么需要帮助的吗？',
    timestamp: Date.now()
  }
])

const features = [
  {
    id: 1,
    title: '智能报表生成',
    description: '通过自然语言描述生成专业的业务报表',
    icon: Document,
    color: '#409EFF'
  },
  {
    id: 2,
    title: '数据分析助手',
    description: '智能分析业务数据，提供深度 insights',
    icon: TrendCharts,
    color: '#67C23A'
  },
  {
    id: 3,
    title: '业务流程助手',
    description: '协助处理订单、库存等业务流程',
    icon: ShoppingCart,
    color: '#E6A23C'
  },
  {
    id: 4,
    title: '财务计算助手',
    description: '自动计算财务数据，生成财务报表',
    icon: Money,
    color: '#F56C6C'
  },
  {
    id: 5,
    title: '系统配置助手',
    description: '帮助配置系统参数和功能设置',
    icon: Setting,
    color: '#909399'
  },
  {
    id: 6,
    title: '库存管理助手',
    description: '智能库存分析和补货建议',
    icon: Box,
    color: '#9C27B0'
  }
]

const suggestions = [
  {
    id: 1,
    title: '生成销售报表',
    description: '生成最近一个月的销售数据报表',
    icon: Document,
    action: 'generate_sales_report'
  },
  {
    id: 2,
    title: '库存分析',
    description: '分析当前库存状况',
    icon: Box,
    action: 'analyze_inventory'
  },
  {
    id: 3,
    title: '财务概览',
    description: '查看财务概览和关键指标',
    icon: Money,
    action: 'view_finance'
  }
]

const handleFeature = (feature) => {
  ElMessage.info(`打开功能：${feature.title}`)
  showAssistantDialog.value = true
}

const executeSuggestion = (suggestion) => {
  assistantInput.value = suggestion.description
  sendAssistantMessage()
}

const sendAssistantMessage = () => {
  if (!assistantInput.value.trim()) return
  
  assistantMessages.value.push({
    role: 'user',
    content: assistantInput.value,
    timestamp: Date.now()
  })
  
  const userMessage = assistantInput.value
  assistantInput.value = ''
  
  // 模拟AI回复
  setTimeout(() => {
    assistantMessages.value.push({
      role: 'assistant',
      content: `我已经理解了您的需求："${userMessage}"。正在为您处理...`,
      timestamp: Date.now()
    })
  }, 1000)
}

const formatTime = (timestamp) => {
  const date = new Date(timestamp)
  return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
}
</script>

<style scoped lang="scss">
.ai-assistant-page {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;

  h3 {
    margin: 0;
    color: #303133;
  }
}

.assistant-features {
  .feature-card {
    margin-bottom: 20px;
    cursor: pointer;
    transition: transform 0.2s;

    &:hover {
      transform: translateY(-4px);
    }
  }

  .feature-icon {
    text-align: center;
    margin-bottom: 16px;
  }

  .feature-title {
    font-size: 18px;
    font-weight: 500;
    text-align: center;
    margin-bottom: 8px;
    color: #303133;
  }

  .feature-desc {
    font-size: 14px;
    color: #909399;
    text-align: center;
    margin-bottom: 16px;
    line-height: 1.6;
  }

  .feature-actions {
    text-align: center;
  }
}

.assistant-dialog {
  .assistant-content {
    display: grid;
    grid-template-columns: 300px 1fr;
    gap: 20px;
    height: 600px;
  }

  .assistant-panel {
    border-right: 1px solid #e5e5e5;
    padding-right: 20px;
  }

  .assistant-suggestions {
    .suggestions-title {
      font-size: 14px;
      font-weight: 500;
      margin-bottom: 12px;
      color: #303133;
    }

    .suggestion-item {
      display: flex;
      gap: 12px;
      padding: 12px;
      border: 1px solid #e5e5e5;
      border-radius: 6px;
      margin-bottom: 8px;
      cursor: pointer;
      transition: all 0.2s;

      &:hover {
        border-color: #409EFF;
        background: #f0f9ff;
      }
    }

    .suggestion-icon {
      font-size: 20px;
      color: #409EFF;
      flex-shrink: 0;
    }

    .suggestion-content {
      flex: 1;
    }

    .suggestion-title {
      font-size: 14px;
      font-weight: 500;
      margin-bottom: 4px;
      color: #303133;
    }

    .suggestion-desc {
      font-size: 12px;
      color: #909399;
    }
  }

  .assistant-chat {
    display: flex;
    flex-direction: column;
    height: 100%;
  }

  .chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: #f5f5f5;
    border-radius: 6px;
    margin-bottom: 16px;
  }

  .message {
    display: flex;
    gap: 12px;
    margin-bottom: 16px;

    &.user {
      flex-direction: row-reverse;

      .message-content {
        background: #409EFF;
        color: #fff;
      }
    }

    &.assistant {
      .message-content {
        background: #fff;
      }
    }
  }

  .message-avatar {
    flex-shrink: 0;
  }

  .message-content {
    max-width: 70%;
    padding: 10px 14px;
    border-radius: 8px;
  }

  .message-text {
    margin-bottom: 4px;
    line-height: 1.5;
  }

  .message-time {
    font-size: 11px;
    opacity: 0.7;
  }

  .chat-input {
    // 输入框样式
  }
}
</style>
